package com.gmako.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gmako.dao.BaseDAO;
import com.gmako.utils.Pager;

@SuppressWarnings("unchecked")
public class BaseDAOImpl extends HibernateDaoSupport implements BaseDAO{

	public void callProcedure(String call) {
		SQLQuery query = this.getSession().createSQLQuery(call);    
		query.executeUpdate();		
	}

	public int countAll(String clazz) {
		final String hql = "select count(*) from "+clazz;
		Long count = (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				query.setMaxResults(1);
				return query.uniqueResult();
			}
		});	
		return count.intValue();
	}
	public int countAllByhouseinfoid(String clazz,int houseinfoID) {
		final String hql = "select count(*) from "+clazz+" where houseinfoID = " + houseinfoID;
		Long count = (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				query.setMaxResults(1);
				return query.uniqueResult();
			}
		});	
		return count.intValue();
	}
	public void delete(Object obj) {
		getHibernateTemplate().delete(obj);		
		
	}

	public void deleteById(Class clazz, Serializable id) {
		getHibernateTemplate().delete(getHibernateTemplate().load(clazz, id));			
	}

	public List findByProperty(String clazz, String propertyName, Object value) {
		String hql = "from "+clazz+" as model where model."	+ propertyName + "= ?";
		return getHibernateTemplate().find(hql, value);
	}

	public List findInProperty(String clazz, String propertyName, String value) {
		String hql = "from "+clazz+" as model where model." + propertyName + " in (" + value + ")";
		return getHibernateTemplate().find(hql);
	}
	
	public List findLikeProperty(String clazz, String propertyName, String value) {
		String hql = "from "+clazz+" as model where model."	+ propertyName + " like ?";
		return getHibernateTemplate().find(hql, value);
	}

	public List listAll(String clazz) {
		return getHibernateTemplate().find("from "+clazz);
	}

	public Object loadObject(String hql) {
		final String hql1 = hql;
		Object obj = null;
		List list = getHibernateTemplate().find(hql1);
		if(list.size()>0)obj=list.get(0);	
		return obj;
	}

	public void save(Object obj) {
		getHibernateTemplate().save(obj);		
	}

	public void saveOrUpdate(Object obj) {
		getHibernateTemplate().saveOrUpdate(obj);
	}

	public void saveOrUpdateAll(Collection collection) {
		getHibernateTemplate().saveOrUpdateAll(collection);
		
	}

	public int update(String hql) {
		final String hql1 = hql; 
		return ((Integer)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql1);
				return query.executeUpdate();
			}
		})).intValue();	
	}
	
	public Pager findPagerAll(final int pageSize, final int pageNo, Class clazz) {
		Criteria queryCriteria = this.getSession().createCriteria(clazz);
		List<Object> result = queryCriteria.addOrder(Order.desc("id"))
		.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).setCacheable(true).list();
		return new Pager(pageSize, pageNo,this.countAll(clazz.getName()), result);
	}
	
	public Pager findPagerByHql(final int pageSize, final int pageNo,String sqlCount,String SqlList){
			Query queryCount = this.getSession().createQuery(sqlCount);
			Query query = this.getSession().createQuery(SqlList).setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize);
			List result =query.list();
			int rowCount= queryCount.list().hashCode();
			return new Pager(pageSize, pageNo, rowCount, result);
	}
	
	/**
	 * 源生SQL语句查询分页
	 */
	public Pager findPagerSql(final int pageSize, final int pageNo,String sqlCount,String SqlList,Class clas) {
		Session session = this.getSession();
		Query queryCount = session.createSQLQuery(sqlCount);
		Query query = session.createSQLQuery(SqlList).addEntity(clas);
		query.setFirstResult(pageSize * (pageNo - 1));
		query.setMaxResults(pageSize);
		List result = query.list();
		return new Pager(pageSize, pageNo, queryCount.list().get(0).hashCode(), result);

	}
	public void update(Object obj) {
		getHibernateTemplate().update(obj);
	}

	/**
	 * sql查询
	 * @author董志强
	 * 2012-7-17上午09:39:32
	 * @param pageSize
	 * @param pageNo
	 * @param sqlCount
	 * @param SqlList
	 * @return
	 */
	public List findBysql(String sql){
		Session session = this.getSession();
		Query query = session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		List<HashMap<Object, Object>> result = query.list();
		return result;
	}
	
	
	/**
	 * 分页多表查询
	 * @param pageSize
	 * @param pageNo
	 * @param sqlCount
	 * @param SqlList
	 * @return
	 */
	public Pager findPagerSqlForJoin(final int pageSize, final int pageNo,String sqlCount,String SqlList) {
		Session session = this.getSession();
		Query queryCount = session.createSQLQuery(sqlCount);
		Query query = session.createSQLQuery(SqlList).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(pageSize * (pageNo - 1)).setMaxResults(pageSize);
		List<HashMap<Object, Object>> result = query.list();
		return new Pager(pageSize, pageNo, queryCount.list().get(0).hashCode(), result);

	}

}
